package guiBeatporty;
import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.LineBorder;

import Controle.Controlador;
import Controle.TabelaDeMusicasModel;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Vector;


public class CadastroMusica extends JInternalFrame {
	private JTextField m_txtNome;
	private JTextField m_txtArtista;
	private JTextField m_txtGravadora;
	private JTextField m_txtAno;
	private JTable table;
	
	private JComboBox m_cbEstilo;
	
	DefaultTableModel model;

	/**
	 * Create the frame.
	 */
	public CadastroMusica() {
		setBorder(new LineBorder(new Color(0, 0, 0)));
		setBounds(100, 100, 595, 400);
		
		JPanel panel = new JPanel();
		
		JScrollPane scrollPane = new JScrollPane();
		
		String[] estilo = {Controlador.strHouse,Controlador.strTechno,Controlador.strReggae,Controlador.strHipHop,Controlador.strRock}; 
		m_cbEstilo = new JComboBox(estilo);
		
		table = new JTable();
		
		table.setModel(model = new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Nome", "Artista", "Gravadora", "Lan\u00E7amento","Estilo"
			}
		));
		scrollPane.setViewportView(table);
		
		JLabel lblCadastroMsica = new JLabel("Cadastro M\u00FAsica");
		lblCadastroMsica.setBounds(208, 0, 159, 27);
		lblCadastroMsica.setFont(new Font("Tahoma", Font.PLAIN, 22));
		
		JLabel lblNome = new JLabel("Nome:");
		lblNome.setBounds(10, 39, 41, 17);
		lblNome.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		m_txtNome = new JTextField();
		m_txtNome.setBounds(93, 38, 274, 20);
		m_txtNome.setColumns(10);
		
		JLabel lblArtista = new JLabel("Artista:");
		lblArtista.setBounds(10, 70, 43, 17);
		lblArtista.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		m_txtArtista = new JTextField();
		m_txtArtista.setBounds(93, 69, 274, 20);
		m_txtArtista.setColumns(10);
		
		JLabel lblGravadora = new JLabel("Gravadora:");
		lblGravadora.setBounds(10, 102, 79, 14);
		lblGravadora.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		m_txtGravadora = new JTextField();
		m_txtGravadora.setBounds(93, 100, 274, 20);
		m_txtGravadora.setColumns(10);
		panel.setLayout(null);
		panel.add(lblCadastroMsica);
		panel.add(lblNome);
		panel.add(m_txtNome);
		panel.add(lblArtista);
		panel.add(m_txtArtista);
		panel.add(lblGravadora);
		panel.add(m_txtGravadora);
		
		JLabel lblAno = new JLabel("Lan\u00E7amento:");
		lblAno.setBounds(10, 133, 79, 14);
		lblAno.setFont(new Font("Tahoma", Font.PLAIN, 12));
		panel.add(lblAno);
		
		m_txtAno = new JTextField();
		m_txtAno.setBounds(93, 131, 201, 20);
		panel.add(m_txtAno);
		m_txtAno.setColumns(10);
		
		
		m_cbEstilo.setBounds(443, 69, 104, 20);
		panel.add(m_cbEstilo);
		
		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(model.getDataVector().size() != -1){
					Vector linhas = new Vector();
					for(int i = 0; i < model.getDataVector().size(); i++){
	 					 linhas = (Vector) model.getDataVector();
							 Vector strings = new Vector();
							 strings = (Vector) linhas.get(i);
							 
							 String nome = (String) strings.get(0);
							 String artista = (String) strings.get(1);
							 String gravadora = (String) strings.get(2);
							 String lancamento = (String) strings.get(3);
							 String estilo = (String) strings.get(4);
							 
							 try {
								Controlador.getControlador().novaMusica(nome, artista, gravadora, estilo, lancamento);
							} catch (Exception e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
					}
					try {
						Controlador.getControlador().salvarDados();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				MainFrame.atualizarTabelas();
				dispose();
				MainFrame.resizeMainFrame(true);
			}
		}
		});
		
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(255)
							.addComponent(btnSalvar))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(panel, GroupLayout.DEFAULT_SIZE, 583, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 583, Short.MAX_VALUE)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 199, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 129, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnSalvar)
					.addContainerGap(24, Short.MAX_VALUE))
		);
		
		JLabel lblEstilo = new JLabel("Estilo:");
		lblEstilo.setBounds(395, 71, 38, 14);
		lblEstilo.setFont(new Font("Tahoma", Font.PLAIN, 12));
		panel.add(lblEstilo);
		
		JButton btnAdicionar = new JButton("Adicionar");
		btnAdicionar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				String nome = m_txtNome.getText();
				String artista = m_txtArtista.getText();
				String gravadora = m_txtGravadora.getText();
				String lancamento = null;

						lancamento = m_txtAno.getText();
						String estilo = (String) m_cbEstilo.getSelectedItem();
						
						try {
							if(!Controlador.getControlador().isValidDate(lancamento)){
								return;
								
							}
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						Object[] data = {nome,artista,gravadora,lancamento,estilo};
						
						model.addRow(data);
						model.fireTableDataChanged();
						
						m_txtNome.setText("");
						m_txtArtista.setText("");
						m_txtGravadora.setText("");
						m_txtAno.setText("");	
			}
		});
		btnAdicionar.setBounds(375, 130, 89, 23);
		panel.add(btnAdicionar);
		
		JButton btnExcluir = new JButton("Excluir");
		btnExcluir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				model.removeRow(table.getSelectedRow());
			}
		});
		btnExcluir.setBounds(471, 130, 89, 23);
		panel.add(btnExcluir);
		getContentPane().setLayout(groupLayout);

	}
}
